﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Framework;

namespace PasswordProvider.ViewModel
{
    public class ViewOptionsViewModel : VisualViewModel, ICommitChanges
    {
        internal ViewOptionsViewModel()
            : base(Strings.ViewOptions_DisplayName, Strings.ViewOptions_ToolTip)
        {
            SetCheckBoxEnabledStatus(RunInSystemTray.IsChecked);
        }

        #region Public

        public ListSelectViewModel<string> SkinSelector
        {
            get
            {
                if (_themeSelector == null)
                {
                    _themeSelector = new ListSelectViewModel<string>(
                        "Skin", "Change the application's colors",
                        new List<string> { "Default", "Black", "Green", "Khaki", "Red" }, 
                        Settings.Default.CurrentSkin);
                    _themeSelector.SelectedItemChanged += 
                        (s,e)=>
                        {
                           // HACK: when navigating away from the page, the view's combobox set's this value to null. 
                            // Null is an invalid value, so just don't allow it. I don't understand why.
                            if (e.NewValue == null)
                                e.Cancel = true;
                            else
                            {
                                // Set the property in real time - so user gets a preview of what the skin looks like.
                                Settings.Default.CurrentSkin = e.NewValue;
                            }
                        };
                }
                return _themeSelector;
            }
        } private ListSelectViewModel<string> _themeSelector;



        /// <summary>
        /// List view sorting method option
        /// </summary>
        public EnumSelectorViewModel ListViewSortMethod
        {
            get 
            {
                if (_listViewSortMethod == null)
                {
                    _listViewSortMethod = new EnumSelectorViewModel(
                        Strings.ViewOptions_ListViewSortMethod_DisplayString,
                        Strings.ViewOptions_ListViewSortMethod_ToolTip,
                        Settings.Default.ListViewSortMethod);
                    _listViewSortMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewSortMethod_Alphabetical,
                            Strings.ViewOptions_ListViewSortMethod_Alphabetical_ToolTip,
                            ViewModel.ListViewSortMethod.AlphabeticallyByName));
                    _listViewSortMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewSortMethod_ByAccessFrequency,
                            Strings.ViewOptions_ListViewSortMethod_ByAccessFrequency_ToolTip,
                            ViewModel.ListViewSortMethod.ByAccessCount));
                    _listViewSortMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewSortMethod_ByLastAccessDate,
                            Strings.ViewOptions_ListViewSortMethod_LastAccessDate_ToolTip,
                            ViewModel.ListViewSortMethod.ByLastAccessDate));
                }
                return _listViewSortMethod;
            }
        } EnumSelectorViewModel _listViewSortMethod;

        /// <summary>
        /// List view grouping method option
        /// </summary>
        public EnumSelectorViewModel ListViewGroupMethod
        {
            get
            {
                if (_listViewGroupingMethod == null)
                {
                    _listViewGroupingMethod = new EnumSelectorViewModel(
                        Strings.ViewOptions_ListViewGroupingMethod_DisplayString,
                        Strings.ViewOptions_ListViewGroupingMethod_ToolTip,
                        Settings.Default.ListViewGroupMethod);
                    _listViewGroupingMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewGroupingMethod_NoGrouping,
                            Strings.ViewOptions_ListViewGroupingMethod_NoGrouping_ToolTip,                            
                            ViewModel.ListViewGroupMethod.NoGrouping));
                    _listViewGroupingMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewGroupingMethod_ByTag,
                            Strings.ViewOptions_ListViewGroupingMethod_ByTag_ToolTip,
                            ViewModel.ListViewGroupMethod.GroupByTag));
                    _listViewGroupingMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.ViewOptions_ListViewGroupingMethod_BySiteType,
                            Strings.ViewOptions_ListViewGroupingMethod_BySiteType_ToolTip,
                            ViewModel.ListViewGroupMethod.GroupByType));
                }
                return _listViewGroupingMethod;
            }
        } EnumSelectorViewModel _listViewGroupingMethod;

        
        /// <summary>
        /// MinimizeToNotificationArea option
        /// </summary>
        public ToggleViewModel MinimizeToNotificationArea
        {
            get
            {
                if (_minimizeToNotificationArea == null)
                {
                    _minimizeToNotificationArea = new ToggleViewModel(
                        Strings.ViewOptions_MinimizeToNotificationArea_DisplayString,
                        Strings.ViewOptions_MinimizeToNotificationArea_ToolTip,
                        Settings.Default.MinimizeToSystemTray);
                    _minimizeToNotificationArea.IsCheckedChanged += 
                        new ValueChangedEventHandler<bool>(MinimizeToNotificationArea_IsCheckedChanged);

                }
                return _minimizeToNotificationArea;
            }
        } private ToggleViewModel _minimizeToNotificationArea;


        /// <summary>
        /// AlwaysOnTop option
        /// </summary>
        public ToggleViewModel AlwaysOnTop
        {
            get
            {
                if (_alwaysOnTop == null)
                {
                    _alwaysOnTop = new ToggleViewModel(
                        Strings.ViewOptions_AlwaysOnTop_DisplayString,
                        Strings.ViewOptions_AlwaysOnTop_ToolTip,
                        Settings.Default.AlwaysOnTop);
                    _alwaysOnTop.IsCheckedChanged += 
                        new ValueChangedEventHandler<bool>(AlwaysOnTop_IsCheckedChanged);

                }
                return _alwaysOnTop;
            }
        } private ToggleViewModel _alwaysOnTop;


        /// <summary>
        /// PinWhenDeactivated option
        /// </summary>
        public ToggleViewModel PinWhenDeactivated
        {
            get
            {
                if (_pinWhenDeactivated == null)
                {
                    _pinWhenDeactivated = new ToggleViewModel(
                        Strings.ViewOptions_PinWhenDeactivated_DisplayString,
                        Strings.ViewOptions_PinWhenDeactivated_ToolTip,
                        Settings.Default.PinWindowOpen);
                    _pinWhenDeactivated.IsCheckedChanged += 
                        new ValueChangedEventHandler<bool>(PinWhenDeactivated_IsCheckedChanged);
                }
                return _pinWhenDeactivated;
            }
        } private ToggleViewModel _pinWhenDeactivated;

        /// <summary>
        /// RunInSystemTray option
        /// </summary>
        public ToggleViewModel RunInSystemTray
        {
            get
            {
                if (_runInSystemTray == null)
                {
                    _runInSystemTray = new ToggleViewModel(
                        Strings.ViewOptions_RunInSystemTray_DisplayString,
                        Strings.ViewOptions_RunInSystemTray_ToolTip,
                        Settings.Default.RunInSystemTray); 
                    _runInSystemTray.IsCheckedChanged += 
                        new ValueChangedEventHandler<bool>(RunInSystemTray_IsCheckedChanged);                    
                }
                return _runInSystemTray;
            }
        } private ToggleViewModel _runInSystemTray;

        /// <summary>
        /// ShowLoginSiteIcons option.
        /// </summary>
        public ToggleViewModel ShowLogOnSiteIcons
        {
            get 
            {
                if (_showLogOnSiteIcons == null)
                {
                    _showLogOnSiteIcons = new ToggleViewModel(
                        Strings.ViewOptions_ShowLogOnSiteIcons_DisplayString,
                        Strings.ViewOptions_ShowLogOnSiteIcons_ToolTip,
                        Settings.Default.ShowSiteIcons);

                }
                return _showLogOnSiteIcons; 
            }
            set { _showLogOnSiteIcons = value; }
        } private ToggleViewModel _showLogOnSiteIcons;

        #endregion // public interface

        #region ICommitChanges

        public void CommitChanges()
        {
            // Set ShowLoginSiteIcons. 
            if (this.ShowLogOnSiteIcons.IsChanged)
                Settings.Default.ShowSiteIcons = ShowLogOnSiteIcons.IsChecked;
            // Set ListViewGroupMethod
            if (this.ListViewGroupMethod.IsChanged)
                Settings.Default.ListViewGroupMethod = (ListViewGroupMethod)ListViewGroupMethod.EnumValue;
            // Set ListViewSortMethod
            if (this.ListViewSortMethod.IsChanged)
                Settings.Default.ListViewSortMethod = (ListViewSortMethod)ListViewSortMethod.EnumValue;
            // Skin
            if (SkinSelector.SelectedItem != Settings.Default.CurrentSkin)
                Settings.Default.CurrentSkin = SkinSelector.SelectedItem;
        }

        #endregion // Overrides

        #region Private Helper Methods

        void MinimizeToNotificationArea_IsCheckedChanged(bool isChecked)
        {
            Settings.Default.MinimizeToSystemTray = isChecked;
        }

        void AlwaysOnTop_IsCheckedChanged(bool isChecked)
        {
            Settings.Default.AlwaysOnTop = isChecked;
        }

        void PinWhenDeactivated_IsCheckedChanged(bool isChecked)
        {
            Settings.Default.PinWindowOpen = isChecked;
        }

        void RunInSystemTray_IsCheckedChanged(bool isChecked)
        {
            bool runInSystemTray = isChecked;

            Settings.Default.RunInSystemTray = runInSystemTray;
            SetCheckBoxEnabledStatus(runInSystemTray);
                 
        }

        private void SetCheckBoxEnabledStatus(bool runInSystemTray)
        {
            MinimizeToNotificationArea.IsEnabled = !runInSystemTray;
            AlwaysOnTop.IsEnabled = !runInSystemTray;
            PinWhenDeactivated.IsEnabled = runInSystemTray;     
        }

        #endregion


    }
}
